const express = require('express');

const app = express();

app.use(express.json())

app.use(express.urlencoded({extended:false}))

const fs = require('fs');

const path = require('path');

let userlist = [
    {
        name:'lixd',
        pwd:'123',
        email:'123@qq.com'
    },
    {
        name:'zs',
        pwd:'123',
        email:'123@qq.com'
    }
]

app.post('/login',(req,res) => {
    let {name,pwd} = req.body;

    let isSuccess = userlist.some(item => item.name === name && item.pwd === pwd);

    if(isSuccess){
        res.send({code:1,msg:'登录成功'})
    }else{
        res.send({code:0,msg:'登录失败'})    
    }
    
})

let questionList = [
    {
        type: 'input',
        message: '姓名:',
        name: 'name'
    },
    {
        type: 'list',
        message: '性别',
        choices: [
            "男",
            "女"
        ],
        name: 'sex'
    }
    ,
    {
        type: 'input',
        message: '家庭住址',
        name: 'address'
    },
    {
        type: 'input',
        message: '年龄',
        name: 'age'
    }
]

app.get('/list',(req,res) => {
    res.send({questionList})
})

app.post('/answer',(req,res) => {
    let con = JSON.parse(fs.readFileSync(path.join(__dirname,'answer.json')));

    con.push(req.body);

    fs.writeFileSync(path.join(__dirname,'answer.json'),JSON.stringify(con))
    res.send({code:1,msg:'提交成功'})
})

app.listen(8000,() => {
    console.log("服务器启动成功")
})